(function () {
    const BASE_URL = "https://www.ykjhr.com";
    const IS_MEDIT = location.origin.includes("medit") ||
        location.origin.includes("mshare");

    function generateRandomString() {
        let randomString = "";
        while (randomString.length < 32) {
            randomString += Math.random().toString(36).substr(2);
        }
        return randomString.substr(0, 32);
    }

    function getRedirect(isNeed = fasle) {
        let redirect = location.origin + location.pathname;
        // 三维编辑器特殊处理
        if (IS_MEDIT) {
            if (isNeed) {
                redirect += `?e=${encodeURIComponent(
                    location.search.slice(1)
                )}`;
            } else {
                const queryObj = {};
                const querys = location.search.slice(1).split("&");
                querys.forEach((item) => {
                    item.replace(/(.+)=(.+)/, function (_, $1, $2) {
                        queryObj[$1] = decodeURIComponent($2);
                    });
                });
                redirect += `?${queryObj["e"]}`;
            }
        }
        return redirect;
    }

    function redirectToSso() {
        window.location.replace(
            BASE_URL + "/sso?redirect=" + getRedirect(true)
        );
    }

    window.SSO = function (callback) {
        const s = new URLSearchParams(location.search);
        if (s.get("ticket")) {
            sessionStorage.setItem("ticket", s.get("ticket"));
            window.location.replace(getRedirect(false));
        }
        const ticket = sessionStorage.getItem("ticket") || "";
        if (ticket) {
            var xhr = new XMLHttpRequest();
            xhr.open(
                "GET",
                BASE_URL + "/api/sso/verify?ticket=" + ticket,
                true
            );
            xhr.responseType = "json";
            xhr.onload = function () {
                if (xhr.status >= 200 && xhr.status < 300) {
                    let res = xhr.response;
                    if (res.data && res.data.id) {
                        sessionStorage.setItem("ticket", ticket);
                        typeof callback === "function" && callback(res.data);
                    } else {
                        redirectToSso();
                    }
                }
            };
            xhr.send();
        } else {
            redirectToSso();
        }
    };

    window.SSOOpen = function (callback) {
        let code = generateRandomString();
        let url = BASE_URL + "/auth?code=" + code;
        const onMsg = function (event) {
            if (event.data && event.data.code === code) {
                sessionStorage.setItem("ticket", event.data.token);
                typeof callback === "function" && callback(event.data);
                window.removeEventListener("message", onMsg);
            }
        }

        window.open(url, "_blank");
        window.addEventListener("message", onMsg);
    }

    function IsPhone() {
        return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
    }

    window.WaterMark = function () {
        const waterMarkDiv = document.createElement("div");
        waterMarkDiv.style.cssText =
            "position: fixed;top: 0;left: 0;width: 100vw;height:100vh;background: url(https://yzpt-resources.oss-cn-hangzhou.aliyuncs.com/web/images/watermark.png) repeat 100%/100%;z-index: 99; pointer-events: none;";
        document.body.appendChild(waterMarkDiv);
    };

    window.CenterWaterMark = function () {
        const waterMarkDiv = document.createElement("div");
        waterMarkDiv.className = "center-watermark";
        const watermarkImage = document.createElement("img");
        watermarkImage.src =
            "https://yzpt-resources.oss-cn-hangzhou.aliyuncs.com/web/images/logo_white.png";
        watermarkImage.style.cssText = `position: fixed;top: 50%;left: 50%;width: ${IsPhone() ? "50vw" : "15vw"
            };z-index: 99; pointer-events: none;user-select: none;transform: translate(-50%, -50%);opacity: 0.5;`;
        waterMarkDiv.appendChild(watermarkImage);
        document.body.appendChild(waterMarkDiv);
    };
})();
